﻿Imports System.Xml

Public Class ClsStation
    Private m_Whendate As Nullable(Of DateTime)
    Private m_WhereName As String
    Private m_WhereLon As Nullable(Of Double)
    Private m_WhereLat As Nullable(Of Double)
    Private m_WhereAlt As String
    Private m_WhereDop As String
    Private m_WhereAccuracy As String
    Private m_WhereSiteId As Nullable(Of Integer)
    Private m_WhereSiteName As String
    Private m_WhereComments As String
    Private m_WhoFW1ID As Nullable(Of Integer)
    Private m_WhoFW1Name As String
    Private m_WhoFW2ID As Nullable(Of Integer)
    Private m_WhoFW2Name As String
    Private m_WhoFWNb As Nullable(Of Integer)
    Private m_WhatLst As List(Of ClsObservation)


    Public Function ExportStaToXML(ByRef writer As XmlWriter) As XmlWriter
        Try
            writer.WriteStartElement("RELEVE")
            writer.WriteStartElement("WHEN")
            writer.WriteElementString("DATE", Me.m_Whendate)
            writer.WriteEndElement()
            writer.WriteStartElement("WHERE")
            writer.WriteElementString("NAME", Me.m_WhereName)
            writer.WriteElementString("LONGITUDE", ChangeDecimalSeparator(Me.m_WhereLon.ToString, ",", "."))
            writer.WriteElementString("LATITUDE", ChangeDecimalSeparator(Me.m_WhereLat.ToString, ",", "."))
            writer.WriteElementString("ELEVATION", Me.m_WhereAlt)
            writer.WriteElementString("DOP", Me.m_WhereDop)
            'writer.WriteElementString("ACCURACY", ChangeDecimalSeparator(Me.m_WhereAccuracy.ToString, ",", "."))
            writer.WriteElementString("ACCURACY", Me.m_WhereAccuracy)
            writer.WriteElementString("SITE_ID", Me.m_WhereSiteId.ToString)
            writer.WriteElementString("SITE_NAME", Me.m_WhereSiteName)
            writer.WriteElementString("COMMENTS", Me.m_WhereComments)
            writer.WriteEndElement()
            writer.WriteStartElement("WHO")
            writer.WriteAttributeString("NB", Me.m_WhoFWNb.ToString)
            writer.WriteElementString("FIELDWORKER1_ID", Me.m_WhoFW1ID.ToString)
            writer.WriteElementString("FIELDWORKER1_NAME", Me.m_WhoFW1Name)
            writer.WriteElementString("FIELDWORKER2_ID", Me.m_WhoFW2ID.ToString)
            writer.WriteElementString("FIELDWORKER2_NAME", Me.m_WhoFW2Name)
            writer.WriteEndElement()
            'WHAT
            For Each obs As ClsObservation In m_WhatLst
                writer = obs.ExportObsToXML(writer)
            Next
            writer.WriteEndElement()
            Return writer
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function

    '##########################################################################################################################
    '                       CONSTRUCTEURS et Cie
    '##########################################################################################################################
    Public Sub New()
        Me.m_WhatLst = New List(Of ClsObservation)
    End Sub

    Public Sub New(ByVal ddate As Nullable(Of DateTime), _
                   ByVal sWptName As String, ByVal dlon As Double?, ByVal dlat As Double?, ByVal dalt As String, ByVal ddop As String, ByVal daccuracy As String, ByVal dzoneId As Nullable(Of Integer), ByVal dZoneName As String, ByVal sWptCom As String, _
                   ByVal iFw1Id As Nullable(Of Integer), ByVal sFw1 As String, ByVal iFw2Id As Nullable(Of Integer), ByVal sFw2 As String, ByVal iFwNb As Nullable(Of Integer))
        Try
            Me.InitWhen(ddate)
            Me.InitWhere(sWptName, dlon, dlat, dalt, ddop, daccuracy, dzoneId, dZoneName, sWptCom)
            Me.InitWho(iFw1Id, sFw1, iFw2Id, sFw2, iFwNb)
            Me.m_WhatLst = New List(Of ClsObservation)
        Catch ex As Exception
            Throw New Exception("Erreur création station: " & ex.Message)
        End Try

    End Sub

    Public Sub New(ByVal sXmlObservation As String)
        Dim m_xmld As XmlDocument
        Dim m_nodelist As XmlNodeList
        Dim m_node As XmlNode
        Me.m_WhatLst = New List(Of ClsObservation)
        Try
            'Création du document XML
            m_xmld = New XmlDocument()
            'Chargement du fichier XML
            Dim m_sr As New System.IO.StringReader(sXmlObservation)
            m_xmld.Load(m_sr)

            'Récupération des données WHEN et instanciation des variables
            '<WHEN>
            '   <DATE>30/09/09 10:31:31</DATE> 
            '</WHEN>
            Me.InitWhen(m_xmld.SelectNodes("/RELEVE/WHEN/DATE").Item(0).InnerText)

            'Récupération des données WHERE et instanciation des variables
            '<WHERE>
            '   <NAME>Obs2</NAME> 
            '   <LONGITUDE>5.96985</LONGITUDE> 
            '   <LATITUDE>44.25252667</LATITUDE> 
            '   <ELEVATION>653</ELEVATION> 
            '   <ACCURACY>20</ACCURACY> 
            '   <SITE_ID>3</SITE_ID> 
            '   <SITE_NAME>G289</SITE_NAME> 
            '   <COMMENTS>buron abandonne</COMMENTS> 
            '</WHERE>
            Me.InitWhere(m_xmld.SelectNodes("/RELEVE/WHERE/NAME").Item(0).InnerText, _
                        GetNodeTypeDouble(m_xmld, "/RELEVE/WHERE/LONGITUDE"), _
                        GetNodeTypeDouble(m_xmld, "/RELEVE/WHERE/LATITUDE"), _
                        m_xmld.SelectNodes("/RELEVE/WHERE/ELEVATION").Item(0).InnerText, _
                        GetNodeTypeDouble(m_xmld, "/RELEVE/WHERE/DOP"), _
                         m_xmld.SelectNodes("/RELEVE/WHERE/ACCURACY").Item(0).InnerText, _
                        GetNodeTypeInteger(m_xmld, "/RELEVE/WHERE/SITE_ID"), _
                        m_xmld.SelectNodes("/RELEVE/WHERE/SITE_NAME").Item(0).InnerText, _
                        m_xmld.SelectNodes("/RELEVE/WHERE/COMMENTS").Item(0).InnerText)

            'Récupération des données WHO et instanciation des variables
            '<WHO NB="3">
            '   <FIELDWORKER1_ID>12</FIELDWORKER1_ID> 
            '   <FIELWORKER1_NAME>Jacky</FIELWORKER1_NAME> 
            '   <FIELDWORKER2_ID>23</FIELDWORKER2_ID> 
            '   <FIELWORKER2_NAME>Maurice</FIELWORKER2_NAME> 
            '</WHO>
            Me.InitWho(GetNodeTypeInteger(m_xmld, "/RELEVE/WHO/FIELDWORKER1_ID"), _
                m_xmld.SelectNodes("/RELEVE/WHO/FIELDWORKER1_NAME").Item(0).InnerText, _
                GetNodeTypeInteger(m_xmld, "/RELEVE/WHO/FIELDWORKER2_ID"), _
                m_xmld.SelectNodes("/RELEVE/WHO/FIELDWORKER2_NAME").Item(0).InnerText, _
                CType(m_xmld.SelectNodes("/RELEVE/WHO").Item(0).Attributes.GetNamedItem("NB").Value, Nullable(Of Integer)))

            'Récupération des données WHAT et création de la liste des observations
            '<WHAT>
            '   <TAXON_ID>3</TAXON_ID> 
            '   <TAXON_NAME>Myotis myotis</TAXON_NAME> 
            '   <NUMBER>2</NUMBER> 
            '   <ABUNDANCY /> 
            '</WHAT>
            'Pour chaque Observation
            m_nodelist = m_xmld.SelectNodes("/RELEVE/WHAT")
            For Each m_node In m_nodelist
                'Création d'une nouvelle observation et Ajout de l'observation à la liste
                AddOneXMLObservation("<WHAT> " & m_node.InnerXml & "</WHAT>")
            Next

        Catch ex As Exception
            Throw New Exception("Erreur chargement fichier observations: " & ex.Message)
        End Try

    End Sub

    'sous "contructeurs"
    Public Sub InitWhen(ByVal ddate As Nullable(Of DateTime))
        Try
            Me.m_Whendate = ddate
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub

    Public Sub InitWhere(ByVal sWptName As String, ByVal dlon As Nullable(Of Double), ByVal dlat As Nullable(Of Double), ByVal dalt As String, ByVal ddop As String, ByVal daccuracy As String, ByVal dzoneId As Nullable(Of Integer), ByVal dZoneName As String, ByVal sWptCom As String)
        Try
            Me.m_WhereName = sWptName
            Me.m_WhereLon = dlon
            Me.m_WhereLat = dlat
            Me.m_WhereAlt = dalt
            Me.m_WhereDop = ddop
            Me.m_WhereAccuracy = daccuracy
            Me.m_WhereSiteId = dzoneId
            Me.m_WhereSiteName = dZoneName
            Me.m_WhereComments = sWptCom
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub

    Public Sub InitWho(ByVal iFw1Id As Nullable(Of Integer), ByVal sFw1 As String, ByVal iFw2Id As Nullable(Of Integer), ByVal sFw2 As String, ByVal iFwNb As Nullable(Of Integer))
        Try
            Me.m_WhoFW1ID = iFw1Id
            Me.m_WhoFW1Name = sFw1
            Me.m_WhoFW2ID = iFw2Id
            Me.m_WhoFW2Name = sFw2
            Me.m_WhoFWNb = iFwNb
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Sub

    '##########################################################################################################################
    '                       ACCESSEURS
    '##########################################################################################################################

    Public Property Whendate() As Nullable(Of DateTime)
        Get
            Return m_Whendate
        End Get
        Set(ByVal value As Nullable(Of DateTime))
            m_Whendate = value
        End Set
    End Property

    Public Property WhereName() As String
        Get
            Return m_WhereName
        End Get
        Set(ByVal value As String)
            m_WhereName = value
        End Set
    End Property

    Public Property WhereLon() As Nullable(Of Double)
        Get
            Return m_WhereLon
        End Get
        Set(ByVal value As Nullable(Of Double))
            m_WhereLon = value
        End Set
    End Property

    Public Property WhereLat() As Nullable(Of Double)
        Get
            Return m_WhereLat
        End Get
        Set(ByVal value As Nullable(Of Double))
            m_WhereLat = value
        End Set
    End Property

    Public Property WhereAlt() As String
        Get
            Return m_WhereAlt
        End Get
        Set(ByVal value As String)
            m_WhereAlt = value
        End Set
    End Property

    Public Property WhereDop() As String
        Get
            Return m_WhereDop
        End Get
        Set(ByVal value As String)
            m_WhereDop = value
        End Set
    End Property

    Public Property WhereSiteId() As Nullable(Of Integer)
        Get
            Return m_WhereSiteId
        End Get
        Set(ByVal value As Nullable(Of Integer))
            m_WhereSiteId = value
        End Set
    End Property

    Public Property WhereSiteName() As String
        Get
            Return m_WhereSiteName
        End Get
        Set(ByVal value As String)
            m_WhereSiteName = value
        End Set
    End Property

    Public Property WhereAccuracy() As String
        Get
            Return m_WhereAccuracy
        End Get
        Set(ByVal value As String)
            m_WhereAccuracy = value
        End Set
    End Property

    Public Property WhereComments() As String
        Get
            Return m_WhereComments
        End Get
        Set(ByVal value As String)
            m_WhereComments = value
        End Set
    End Property

    Public Property WhoFW1ID() As Nullable(Of Integer)
        Get
            Return m_WhoFW1ID
        End Get
        Set(ByVal value As Nullable(Of Integer))
            m_WhoFW1ID = value
        End Set
    End Property

    Public Property WhoFW1Name() As String
        Get
            Return m_WhoFW1Name
        End Get
        Set(ByVal value As String)
            m_WhoFW1Name = value
        End Set
    End Property

    Public Property WhoFW2ID() As Nullable(Of Integer)
        Get
            Return m_WhoFW2ID
        End Get
        Set(ByVal value As Nullable(Of Integer))
            m_WhoFW2ID = value
        End Set
    End Property

    Public Property WhoFW2Name() As String
        Get
            Return m_WhoFW2Name
        End Get
        Set(ByVal value As String)
            m_WhoFW2Name = value
        End Set
    End Property

    Public Property WhoFWNb() As Nullable(Of Integer)
        Get
            Return m_WhoFWNb
        End Get
        Set(ByVal value As Nullable(Of Integer))
            m_WhoFWNb = value
        End Set
    End Property

    Public Property WhatLst() As List(Of ClsObservation)
        Get
            Return m_WhatLst
        End Get
        Set(ByVal value As List(Of ClsObservation))
            m_WhatLst = value
        End Set
    End Property

    Public Function AddOneXMLObservation(ByVal strXMLObs As String) As Integer
        Try
            Dim Obs As New ClsObservation(strXMLObs)
            'Ajout de l'observation à la liste
            m_WhatLst.Add(Obs)
            Return m_WhatLst.LastIndexOf(Obs)
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function

    Public Function AddOneObjectObservation(ByVal oObs As ClsObservation) As Integer
        Try
            m_WhatLst.Add(oObs)
            Return m_WhatLst.LastIndexOf(oObs)
        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try
    End Function
End Class
